package com.cn.ant.modules.wechat.web;

import com.cn.ant.common.persistence.Page;
import com.cn.ant.common.utils.AjaxJson;
import com.cn.ant.common.utils.DateUtils;
import com.cn.ant.common.utils.Identities;
import com.cn.ant.common.utils.StringUtils;
import com.cn.ant.common.web.BaseController;
import com.cn.ant.modules.sys.entity.User;
import com.cn.ant.modules.sys.utils.UserUtils;
import com.cn.ant.modules.wechat.entity.TextTemplate;
import com.cn.ant.modules.wechat.entity.WeixinAccount;
import com.cn.ant.modules.wechat.service.TextTemplateService;
import com.cn.ant.modules.wechat.service.WeixinAccountServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文本消息模板Controller
 *
 * @author AntDream
 * @version 2015-03-22
 */
@Controller
@RequestMapping(value = "${adminPath}/wechat/textTemplate")
public class TextTemplateController extends BaseController {

    @Autowired
    private TextTemplateService textTemplateService;
    @Autowired
    private WeixinAccountServiceImpl accountService;

    @ModelAttribute
    public TextTemplate get(@RequestParam(required = false) String id) {
        if (StringUtils.isNotBlank(id)) {
            return textTemplateService.get(id);
        } else {
            return new TextTemplate();
        }
    }

    @RequestMapping(value = "find")
    @ResponseBody
    public TextTemplate find(@RequestParam(required = true) String id) {
        return textTemplateService.get(id);
    }

    @RequiresPermissions("wechat:textTemplate:view")
    @RequestMapping(value = {"list", ""})
    public String list(TextTemplate textTemplate, HttpServletRequest request, HttpServletResponse response, Model model) {
        User user = UserUtils.getUser();
        Map<String, Object> param = new HashMap<String, Object>();
        Page<TextTemplate> page = textTemplateService.find(new Page<TextTemplate>(request, response), param);
        List<WeixinAccount> accountList = accountService.findByLoginUser();
        model.addAttribute("page", page);
        model.addAttribute("accountList", accountList);
        return "modules/wechat/textTemplateList";
    }

    @RequiresPermissions("wechat:textTemplate:view")
    @RequestMapping(value = "form")
    public String form(TextTemplate textTemplate, Model model) {
        model.addAttribute("textTemplate", textTemplate);
        return "modules/wechat/textTemplateForm";
    }

    @RequiresPermissions("wechat:textTemplate:view")
    @RequestMapping(value = "save")
    @ResponseBody
    public AjaxJson save(TextTemplate textTemplate, Model model) {
        AjaxJson jo = new AjaxJson();
        try {
            if (!beanValidator(model, textTemplate)) {
                jo.setSuccess(false);
                jo.setMsg("数据校验失败,请重试!");
            } else {
                textTemplate.setAddtime(DateUtils.getDate("yyyy-MM-dd HH:mm:ss"));
                if (StringUtils.isBlank(textTemplate.getId())) {
                    textTemplate.setId(Identities.generateUUID());
                    textTemplateService.save(textTemplate);
                } else {
                    textTemplateService.update(textTemplate);
                }
            }
            msg = "保存文本消息模板'" + textTemplate.getTemplatename() + "'成功";
            jo.setMsg(msg);
        } catch (Exception e) {
            e.printStackTrace();
            jo.setSuccess(false);
            jo.setMsg("操作失败,请联系管理员");
        }
        return jo;
    }

    @RequiresPermissions("wechat:textTemplate:view")
    @RequestMapping(value = "delete")
    @ResponseBody
    public AjaxJson delete(String id, RedirectAttributes redirectAttributes) {
        AjaxJson jo = new AjaxJson();
        if (StringUtils.isNotBlank(id)) {
            textTemplateService.delete(id);
            jo.setMsg("删除公众号成功");
        } else {
            jo.setSuccess(false);
            jo.setMsg("参数错误,删除失败");
        }
        return jo;
    }

}
